import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { readFileSync, existsSync } from 'fs';
import { resolve } from 'path';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  console.log('当前工作目录:', process.cwd());
  console.log('mode:', mode);
  
  // 检查.env文件是否存在
  const envPath = resolve(process.cwd(), '.env');
  console.log('.env文件路径:', envPath);
  console.log('.env文件存在:', existsSync(envPath));
  
  if (existsSync(envPath)) {
    console.log('.env文件内容:', readFileSync(envPath, 'utf-8'));
  }
  
  // 加载环境变量
  const env = loadEnv(mode, process.cwd(), '')
  console.log('加载的环境变量结果:', {
    VITE_DEEPSEEK_API_KEY: env.VITE_DEEPSEEK_API_KEY ? '已设置' : '未设置',
    VITE_DEEPSEEK_API_URL: env.VITE_DEEPSEEK_API_URL
  });
  
  return {
    base: '/textcard/',
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    server: {
      port: 3000,
      open: true
    },
    build: {
      outDir: 'dist'
    }
  }
})